<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" type="success" icon="el-icon-edit" @click="handleAdd">
        新增轮播
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="标题">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="跳转地址">
        <template slot-scope="scope">
          <el-link :underline="false" :href="scope.row.url" target="_blank">{{ scope.row.url }}</el-link>
        </template>
      </el-table-column>

      <el-table-column align="center" label="轮播图">
        <template slot-scope="scope">
          <el-image
            style="width: 60px;"
            :src="scope.row.imgUrl"
            :preview-src-list="[scope.row.imgUrl]"
          >
            <div slot="placeholder" class="image-slot" style="width:60px;height:30px;text-align:center;line-height:30px;">
              <i class="dot el-icon-loading" style="font-size:20px;" />
            </div>
          </el-image>
        </template>
      </el-table-column>

      <el-table-column
        prop="isShow"
        label="是否显示"
        align="center"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isShow"
            active-color="#13ce66"
            inactive-color="#ff4949"
            @change="saveCarousel(scope.row)"
          />
        </template>
      </el-table-column>

      <el-table-column width="180px" align="center" label="排序">
        <template slot-scope="scope">
          <span>{{ scope.row.sort }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button-group>
            <el-button :key="scope.row.gid" type="primary" size="mini" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pagesize" @pagination="getData" />

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="100px" style="width: 80%; margin-left:50px;">
        <el-form-item label="名称" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="跳转地址" prop="url">
          <el-input v-model="temp.url" placeholder="请输入跳转地址" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model.number="temp.sort" type="number" />
        </el-form-item>
        <el-form-item label="是否显示" prop="sort">
          <el-switch
            v-model="temp.isShow"
            active-color="#13ce66"
            inactive-color="#ff4949"
          />
        </el-form-item>
        <el-form-item label="轮播图" prop="imgUrl">
          <Upload v-model="temp.imgUrl" remote-path="carousel/{filemd5}{.suffix}" :single-image="true" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="saveCarousel()">
          确认
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import Upload from '@/components/Upload/SingleImage'
import { getCarousel, saveCarousel, delCarousel } from '@/api/carousel'// , editCarousel
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
export default {
  name: 'Carousel',
  components: {
    Pagination,
    Upload
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        pagesize: 50
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      temp: {
        gid: undefined,
        name: '',
        url: '',
        isShow: true,
        sort: 0,
        imgUrl: ''
      },
      rules: {
        name: [{
          required: true,
          message: '请输入名称',
          trigger: 'change'
        }],
        sort: [{
          type: 'number',
          required: true,
          message: '请输入排序号',
          trigger: 'change'
        }],
        type: [{
          type: 'number',
          required: true,
          message: '请选择显示位置',
          trigger: 'change'
        }],
        imgUrl: [{
          required: true,
          message: '请上传轮播图',
          trigger: 'blur'
        }]
      }
    }
  },
  watch: {
    'temp.imgUrl'(newValue) {
      console.log(newValue)
    }
  },
  created() {
    this.getData()
  },
  methods: {
    resetTemp() {
      this.temp = {
        gid: undefined,
        name: '',
        url: '',
        sort: 0,
        isShow: true,
        imgUrl: ''
      }
    },
    handleAdd() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleEdit(row) {
      this.temp = {
        gid: row.gid,
        name: row.name,
        url: row.url,
        sort: row.sort,
        isShow: row.isShow,
        imgUrl: row.imgUrl
      }
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    handleDelete(row) {
      this.$confirm('确定删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delCarousel({ gid: row.gid }).then(res => {
          this.$message.success('删除成功!')
          this.getData()
        })
      })
    },
    getData() {
      this.listLoading = true
      getCarousel({ page: this.listQuery.page, pagesize: this.listQuery.limit }).then(res => {
        this.list = res.result.list
        this.total = res.result.total
        this.listLoading = false
      })
    },
    saveCarousel(data) {
      if (data && data !== '') {
        saveCarousel(data).then(res => {
          this.dialogFormVisible = false
          this.$message.success('保存成功')
          this.getData()
        })
      } else {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            saveCarousel(this.temp).then(res => {
              this.dialogFormVisible = false
              this.$message.success('保存成功')
              this.getData()
            })
          }
        })
      }
    }
  }
}
</script>

<style lang="css">

.edit-input {
  padding-right: 100px;
}

.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
